<?php

class Format extends DirModel
{
	
	public function getUpdateFields()
	{  
		return array(
			'ut_format_name',
			'ut_format_width',
			'ut_format_height',									
		);
	}
	
	public $viewColumns=array(
		'ut_format_name',
		'ut_format_width',
		'ut_format_height',
	);	
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ut_format';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ut_format_name', 'required'),
			array('ut_format_name', 'length', 'max'=>255),
			array('ut_format_width, ut_format_height', 'type', 'type'=>'float'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'paper_expense' => array(self::HAS_ONE, 'PaperExpense', 'ut_format_id'),
			'min_edition' => array(self::STAT,'PaperExpense','ut_format_id','select'=>'min(ut_exp_edition)'),
			//'paper_expense' => array(self::STAT,'PaperExpense','ut_format_id','select'=>'max(ut_exp_edition)','condition'=>'ut_exp_edition<=:edition'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'ut_format_name' => 'Формат',
			'ut_format_width'=>'Ширина',
			'ut_format_height'=>'Высота',
		);
	}
	
	public function paperExpense($edition)
	{
		$edition=max($edition,$this->min_edition);
		$paperExpense = PaperExpense::model('PaperExpense')->find(array(
			'condition'=>'ut_exp_edition<=:edition AND ut_format_id=:format',
			'params'=>array('edition'=>$edition,'format'=>$this->ut_format_id),
			'order'=>'ut_exp_edition desc',
		));
		return $edition*$paperExpense->ut_exp_count/$paperExpense->ut_exp_edition*0.504/1000;
	}

	public function editionA1($edition)
	{
		$edition=max($edition,$this->min_edition);
		$paperExpense = PaperExpense::model('PaperExpense')->find(array(
			'condition'=>'ut_exp_edition<=:edition AND ut_format_id=:format',
			'params'=>array('edition'=>$edition,'format'=>$this->ut_format_id),
			'order'=>'ut_exp_edition desc',
		));
		return $edition*$paperExpense->ut_exp_count/$paperExpense->ut_exp_edition;
	}
	
	public function exp_area($edition)
	{
		$edition=max($edition,$this->min_edition);
		$paperExpense = PaperExpense::model('PaperExpense')->find(array('condition'=>'ut_exp_edition<='.$edition,'order'=>'ut_exp_edition desc'));
		return $paperExpense->ut_exp_count*$paperExpense->base_format->ut_format_width*$paperExpense->base_format->ut_format_length;
	}

}